﻿/*
 * 刘冬 博客园
 * www.cnblogs.com/GoodHelper
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Web.Mvc;

using Client.ControllerBase;
using Host.ModelDTO;

namespace Client.MvcController
{
    public class HomeController : AbstractController
    {
        public ActionResult Index()
        {
            ViewData["Title"] = "Home Page";
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult GetGrid(int page, int rp, string sortname, string sortorder, string qtype, string query)
        {
            long total = 0;
            List<UserDTO> list = this.WcfService.FindUserPageList(out total, page, rp, sortname, sortorder, qtype, query);
            var column = from li in list
                         select new
                         {
                             id = li.UserID,
                             cell = new object[]
                                 {
                                    new { value = li.UserID, name = "UserID" },
                                    new { value = li.Name, name = "Name" },
                                    new { value = li.Sex, name = "Sex" },
                                    new { value = li.Age, name = "Age" }
                                 }
                         };
            object data = new { page = page, total = total, rows = column };

            return Json(data);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(UserDTO user)
        {
            try
            {
                if (user.UserID == null)
                {
                    this.WcfService.SaveUser(user);
                }
                else
                {
                    this.WcfService.UpdateUser(user);
                }
                return Json(new { success = true });
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(new { success = false });
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(List<long> list)
        {
            try
            {
                this.WcfService.DeleteUser(list);
                return Json(new { success = true });
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(new { success = false });
            }
        }

        public ActionResult GetCompany(string q, int p, int s)
        {
            var asd= Request.Form;
            long total = 0;
            List<CompanyDTO> list = this.WcfService.FindCompanyPageList(out total, p, s, "CompanyID", "desc", "Name", q);

            var column = from li in list
                         select new
                         {
                             id = li.CompanyID,
                             name = li.Name
                         };
            object data = new { total = total, results = column };

            return Json(data);
        }       
    }
}
